/*
 * @Author: 一盏青灯欲锁魂
 * @Date: 2021-06-15 16:32:50
 * @LastEditTime: 2021-06-24 10:26:43
 * @Description: index控制器
 * @FilePath: \nest_demo\src\module\index\index.controller.ts
 */
import { Controller, Get, HttpException, HttpStatus, Query, UseFilters } from '@nestjs/common';
import { ExceptionService } from './exception.service';
import { HttpExceptionFilter } from '../../tools/filter/http_exception.filter';

/*
    * 1. 过滤器的使用
    *    1. 引入定义好的filter过滤器'HttpExceptionFilter'
    *    2. 从'@nestjs/common'公共库中引入'UseFilters'、'HttpStatus'、'HttpException'
    *    3. 在方法体中通过'throw new HttpException'来抛出过滤异常的结果
    * 
    * 2. @UseFilters()
    *    局部化使用过滤器，配置其所装饰的class类指定过滤器
    * 
    * 注意：
    *    @UserFilters()中配置指定过滤器需要使用new手动创建出来
*/

// @UseFilters(new HttpExceptionFilter())
@Controller('/exception')
export class ExceptionController {
    constructor(private readonly exceptionService: ExceptionService) { }

    @Get()
    getText(@Query() { id }) {
        if (!id) {
            throw new HttpException(
                { status: HttpStatus.BAD_REQUEST, message: 'id不能为空', error: 'id is required!' },
                HttpStatus.BAD_REQUEST
            )
        };
        return this.exceptionService.getText(id);
    }
}